#include <iostream>
#include <string>
#include <vector>
#include <sqlite3.h>

class sqlitehelper
{
    typedef int(*SqliteCallback)(void*,int,char**,char**);
public:
    sqlitehelper(const std::string dbfile):_dbfile(dbfile),_handler(nullptr){}
    bool open(int safe_level = SQLITE_OPEN_FULLMUTEX)
    {
        // int sqlite3_open_v2(const char*filename,sqlite3 **ppDb,...) 成功返回SQLITE_OK
        int ret = sqlite3_open_v2(_dbfile.c_str(),&_handler,SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | safe_level,nullptr);
        if(ret != SQLITE_OK)
        {
            std::cout<<"打开文件失败"<<std::endl;
            return false;
        }
        return true;
    }
    bool exec(const std::string& sql,SqliteCallback cb,void* arg)
    {
        int ret = sqlite3_exec(_handler,sql.c_str(),cb,arg,nullptr);
        if(ret != SQLITE_OK)
        {
            std::cout<<sql.c_str()<<"操作失败"<<std::endl;
            std::cout<<"错误原因:"<<sqlite3_errmsg(_handler)<<std::endl;
            return false;
        }
        return true;
    }
    bool close()
    {
        if(_handler) sqlite3_close_v2(_handler);
        return true;
    }
private:
    std::string _dbfile;
    sqlite3 *_handler;
};